/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package slevnik.Daemon;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import slevnik.Item;
import slevnik.ItemImpl;

/**
 * Class contains method for parsing coupons.
 * 
 * @author Michal Halaj
 */
public class XMLParser
{
    /*
     * Fills list of items with items parsed from XML
     *
     * @param properties contains properties used for parsing data from XML feeds.
     * @param items list of items, which will be filled by items parsed from XML
     *
     * @return list of new items (deals)
     */
    public static void getNewItems(XMLFeedProperties properties, List<Item> items)
    {
        if(items == null)
        {
            items = new ArrayList<Item>();  
        }     
        
        try
        {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();

            List<String> urls = properties.getUrls();
            for(String url : urls)
            {
                InputStream xmlStream = null;
                try 
                {
                    xmlStream = new URL(url).openStream();
                    Document doc = builder.parse(xmlStream);

                    XPathFactory xpf = XPathFactory.newInstance();
                    XPath xPath = xpf.newXPath();

                    NodeList deals = (NodeList) xPath.evaluate(properties.getDealXPath(), doc, XPathConstants.NODESET);

                    for (int i = 0; i < deals.getLength(); i++)
                    {
                        Item newItem = new ItemImpl();
                        newItem.setCategoryId(1);
                        Element deal = (Element) deals.item(i);
                        newItem.setTitle(xPath.evaluate(properties.getTitleXPath(), deal).trim());
                        newItem.setUrl(xPath.evaluate(properties.getUrlXPath(), deal).trim());
                        newItem.setCity(xPath.evaluate(properties.getCityXPath(), deal).trim());
                        newItem.setOriginalPrice((Double) xPath.evaluate(properties.getOriginalPriceXPath(), deal, XPathConstants.NUMBER));
                        newItem.setFinalPrice((Double) xPath.evaluate(properties.getFinalPriceXPath(), deal, XPathConstants.NUMBER));
                        newItem.setDiscount((Double) xPath.evaluate(properties.getDiscountXPath(), deal, XPathConstants.NUMBER));
                        
                        String categoryName = xPath.evaluate(properties.getCategoryXPath(), deal).trim();
                        
                        if(categoryName != null)
                        {
                            if(categoryName.length() == 0 
                                    ||categoryName.contains("ostat"))
                            {
                                newItem.setCategoryId(1);
                            }
                            else if(categoryName.toLowerCase().contains("jídlo")
                                    || categoryName.toLowerCase().contains("gastron"))
                            {
                                newItem.setCategoryId(2);
                            }
                            else if(categoryName.toLowerCase().contains("droge")
                                    || categoryName.toLowerCase().contains("kosmet")
                                    || categoryName.toLowerCase().contains("péče"))
                            {
                                newItem.setCategoryId(3);
                            }
                            else if(categoryName.toLowerCase().contains("wellness")
                                    || categoryName.toLowerCase().contains("zdravot")
                                    || categoryName.toLowerCase().contains("masáž"))
                            {
                                newItem.setCategoryId(4);
                            }
                            else if(categoryName.toLowerCase().contains("pobyt")
                                    || categoryName.toLowerCase().contains("cestov"))
                            {
                                newItem.setCategoryId(5);
                            }
                            else if(categoryName.toLowerCase().contains("adren")
                                    || categoryName.toLowerCase().contains("sport")
                                    || categoryName.toLowerCase().contains("zážit"))
                            {
                                newItem.setCategoryId(6);
                            }
                            else if(categoryName.toLowerCase().contains("kurz"))
                            {
                                newItem.setCategoryId(8);
                            }
                            else if(categoryName.toLowerCase().contains("zbož"))
                            {
                                newItem.setCategoryId(10);
                            }
                            else
                            {
                                newItem.setCategoryId(1);
                            }
                        }
                        else
                        {
                            newItem.setCategoryId(1);
                        }
                        
                        try
                        {
                                newItem.setCustomers(Integer.parseInt(xPath.evaluate(properties.getCustomersXPath(), deal)));                       
                        }
                        catch(NumberFormatException ex)
                        {
                            Logger.getLogger(XMLParser.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        
                        DateFormat df = new SimpleDateFormat(properties.getDateFormat());

                        try
                        {
                            newItem.setDealStart(df.parse(xPath.evaluate(properties.getDealStartXPath(), deal)));
                        } catch (ParseException ex) {
                            Logger.getLogger(XMLParser.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        
                        try
                        {
                            newItem.setDealEnd(df.parse(xPath.evaluate(properties.getDealEndXPath(), deal)));
                        } catch (ParseException ex) {
                            Logger.getLogger(XMLParser.class.getName()).log(Level.SEVERE, null, ex);
                        }

                        items.add(newItem);
                    }
                }
                catch (SAXException ex) {
                    Logger.getLogger(XMLParser.class.getName()).log(Level.SEVERE, null, ex);
                }
                catch (XPathExpressionException ex)
                {
                    Logger.getLogger(XMLParser.class.getName()).log(Level.SEVERE, null, ex);
                }
                catch (MalformedURLException ex)
                {
                    Logger.getLogger(XMLParser.class.getName()).log(Level.SEVERE, null, ex);
                }
                catch (IOException ex)
                {
                    Logger.getLogger(XMLParser.class.getName()).log(Level.SEVERE, null, ex);
                }
                finally
                {
                    try
                    {
                        if(xmlStream != null)
                        {
                            xmlStream.close();
                        }
                    } 
                    catch (IOException ex)
                    {
                        Logger.getLogger(XMLParser.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        }
        catch (ParserConfigurationException ex)
        {
            Logger.getLogger(XMLParser.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
